/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application;


import exceptions.ExecutionException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Classe static representant la mémoire de l'application
 * @author O.Riad
 * @author R.Dubos
 */
public class Memory {
    
    public final static String GLOBAL_VAR = "GLOBAL";
    private static Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
    private static boolean inst=false;
    
    private static void initMap(){
         map = new HashMap<String, Map<String, Integer>>();
         inst = true;
    }
    
    public static Map<String, Map<String, Integer>> getMap(){
        if(!inst){
            initMap();
            inst = true;
        }
        return map;
    }
    
    public static void add(String methodBlock, String key, Integer value){
        if(!inst){
            initMap();
            inst = true;
        }
        if(!map.containsKey(methodBlock)){
            Map<String, Integer> tmp = new HashMap<String, Integer>();
            tmp.put(key, value);
            map.put(methodBlock, tmp);
        }else{
            map.get(methodBlock).put(key, value);
        }
    }
    
    public static void add(String methodBlock, List<String> params){
        for(int i=0; i<params.size(); i++){
            add(methodBlock, params.get(i), null);
        }
    }
    
    public static int get(String methodBlock, String key) throws ExecutionException{
        return map.get(methodBlock).get(key);
    }
    
    public static boolean exist(String methodBlock, String key){
        if(!inst){
            initMap();
            inst = true;
        }
        return map.get(methodBlock).containsKey(key);
    }
    
    public static void destroy(){
        map.clear();
    }
    
    public static Map<String, Integer> getFunctionMemory(String functionName){
        return map.get(functionName);
    }
   
}
